/**
 *
 * @author ace
 * @create 2017/8/31
 */
class DistancePanel extends BasePanel {

    private icon1: eui.Image;
    private icon2: eui.Image;
    private icon3: eui.Image;
    private icon4: eui.Image;

    private head1: HeadIcon;
    private head2: HeadIcon;
    private head3: HeadIcon;
    private head4: HeadIcon;

    private lab_12: eui.Label;
    private lab_13: eui.Label;
    private lab_14: eui.Label;
    private lab_23: eui.Label;
    private lab_24: eui.Label;
    private lab_34: eui.Label;

    constructor() {
        super();
        this.skinName = "DistancePanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        gameManager.i.addEventListener(GameEventType.UPDATE_DISTANCE_INFO, this.update, this);

    }

    update(data: any) {
        this.updatePlayers();

        let arr: string[] = ['12', '13', '14', '23', '24', '34'];
        let lab: eui.Label;
        for (let i: number = 0; i < arr.length; i++) {
            lab = this['lab_' + arr[i]];
            if (lab) {
                lab.text = '距离未知';
            }
        }

        let len: any;
        for (let key in data.dis) {
            len = data.dis[key];
            key = this.transKey(key);
            lab = this['lab_' + key];
            if (len) {
                lab.text = len < 1000 ? len + '米' : (Math.floor(len / 10) / 100) + '千米';
            }
        }

        let none: number[] = data.none;
        let icon: eui.Image;
        for (let i: number = 0; i < none.length; i++) {
            icon = this['icon' + PublicVal.i.getDir(none[i])];
            if (icon) {
                icon.visible = true;
            }
        }
    }

    updatePlayers() {
        let player: any;
        let head: HeadIcon;
        let icon: eui.Image;
        for (let i: number = 1; i <= game.roomSeat; i++) {
            player = GSData.i.getRoomPlayerByPos(i);

            icon = this['icon' + PublicVal.i.getDir(i)];
            icon.visible = false;

            head = this['head' + PublicVal.i.getDir(i)];
            head.update(player);
        }
    }

    private transKey(key) {
        let arr: number[] = key.split('');
        for (let i: number = 0; i < arr.length; i++) {
            arr[i] = PublicVal.i.getDir(+arr[i]);
        }
        arr.sort();
        return arr.join('');
    }

    show() {
        super.show();

        switch (game.roomSeat) {
            case 2:
                this.skinState = 'two';
                break;
            case 3:
                this.skinState = 'three';
                break;
            case 4:
                this.skinState = 'four';
                break;
        }

        gameManager.socketManager.send(502);
    }
}